﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ex03.GarageLogic
{
    public abstract class Vehicle
    {
        private readonly List<Wheel> r_WheelCollection;
        private string m_ModelName;
        private string m_LicensingNumber;

        public string ModelName
        {
            get { return m_ModelName; }
        }

        public string LicensingNumber
        {
            get { return m_LicensingNumber; }
        }

        public abstract float LeftEnergyPercent { get; }

        public List<Wheel> WheelCollection
        {
            get { return r_WheelCollection; }
        }

        public int WheelCount
        {
            get { return r_WheelCollection.Count; }
        }

        public override bool Equals(object obj)
        {
            bool equals = true;

            Vehicle otherVehicle = obj as Vehicle;
            if (otherVehicle == null)
            {
                equals = false;
            }
            else
            {
                if (m_LicensingNumber != otherVehicle.m_LicensingNumber)
                {
                    equals = false;
                }
            }

            return equals;
        }

        public override int GetHashCode()
        {
            return m_LicensingNumber.GetHashCode();
        }

        protected Vehicle(string i_ModelName, string i_LicensingNumber, List<Wheel> i_Wheels)
        {
            m_ModelName = i_ModelName;
            m_LicensingNumber = i_LicensingNumber;
            r_WheelCollection = new List<Wheel>();

            if (i_Wheels != null)
            {
                foreach (Wheel wheel in i_Wheels)
                {
                    r_WheelCollection.Add(wheel);
                }
            }
        }

        /// <summary>
        /// Inflates all the wheels to their maximal pressure.
        /// </summary>
        public void InflateWheelsToMaximum()
        {
            foreach (Wheel wheel in r_WheelCollection)
            {
                wheel.Inflate(wheel.MaximalAirPressure - wheel.CurrentAirPressure);
            }
        }

        public virtual Dictionary<string, string> GetAllProperties()
        {
            Dictionary<string, string> properties = new Dictionary<string, string>();

            properties.Add("Licensing Number", m_LicensingNumber);
            properties.Add("Model Name", m_ModelName);
            properties.Add("Number Of Wheels", r_WheelCollection.Count.ToString());

            if (r_WheelCollection != null)
            {
                int index = 1;

                foreach (Wheel wheel in r_WheelCollection)
                {
                    Dictionary<string, string> wheelProperties = wheel.GetAllProperties();

                    foreach (KeyValuePair<string, string> property in wheelProperties)
                    {
                        properties.Add(string.Format("Wheel {0} {1}", index.ToString(), property.Key), property.Value);
                    }

                    index++;
                }
            }

            return properties;
        }
    }
}
